<?php

namespace App\Rep;

use App\Models\MaterialDrawLog;
use App\Models\Order;

class MaterialDrawLogRep
{
    /**
     * 获取领料记录列表
     * @param $params
     * @return void
     */
    public function getMaterialDrawLogListRep($params){
        return MaterialDrawLog::Search($params)->with(['order','employee','job_type','material'])
            ->orderBy('id','desc')
            ->paginate($params['per_page']??100);
    }

    /**
     * 添加领料记录
     * @param $params
     * @return void
     */
    public function createMaterialDrawLogRep($params){
        if(isset($params['id']) && !empty($params)){
            $model = MaterialDrawLog::find($params['id']);
        }else{
            $model = new MaterialDrawLog();
        }
        if($params['field'] == 1){
            $order_id = Order::where('serial_number',$params['field_value'])->value('id');
        }elseif ($params['field'] == 2){
            $order_id = Order::where('order_number',$params['field_value'])->value('id');
        }
        if(empty($order_id)){
            return ['code'=>2001,'data'=>$order_id,'msg'=>'订单号或流水号错误'];
        }

        $model->order_id = $order_id;
        $model->employee_user_id = $params['employee_user_id'];
        $model->job_type_id = $params['job_type_id'];
        $model->material_id = $params['material_id'];
        $model->num = $params['num'];
        $model->draw_time = $params['draw_time'];
        $model->save();
        return ['code'=>0,'data'=>$model,'msg'=>'success'];
    }
}
